package com.lyl.momanapp.util.utlis;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;

/**
 * Created by 50393 on 2017/6/26.
 * 首页fragment 管理
 */

public class FragmentController {
    int containerId;
    ArrayList<Fragment> fragments;
    private FragmentManager fm;
    private static FragmentController controller;
    private FragmentController(int containerId, FragmentManager activity, ArrayList<Fragment> fragments ) {
        this.containerId = containerId;
        this.fragments=fragments;
        fm = activity;
        initFragment();
    }
    public static FragmentController getInstance(int containerId, FragmentManager activity,ArrayList<Fragment> fragments) {
        controller = new FragmentController(containerId, activity,fragments);
        return controller;
    }
    private void initFragment() {


        FragmentTransaction ft = fm.beginTransaction();
        for(Fragment fragment : fragments) {
            ft.add(containerId, fragment);
        }
        ft.commit();
    }
    public void showFragment(int position) {
        hideFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.show(fragments.get(position));

        ft.commit();
    }
    public void hideFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        for(Fragment fragment : fragments) {
            if(fragment != null)
                ft.hide(fragment);
        }
        ft.commit();
    }

}